package com.mk.common.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.junit.Test;

import com.aspose.words.Cell;
import com.aspose.words.CellMerge;
import com.aspose.words.Document;
import com.aspose.words.NodeType;
import com.aspose.words.Row;
import com.aspose.words.SaveFormat;
import com.aspose.words.SaveOptions;
import com.aspose.words.Table;

public class AsposeJavaDemo {
	
	/**
	 * 删除不可取，会导出整行缺失一块
	 * @throws Exception
	 */
	@Test
	public void test1() throws Exception {
		String fileName = "工作讨论-输出中间文件.doc";
		String filePath = "D:/HkAppDoc/jwFile/jg_1/tmp/workDiscuss/";
		File fileDir = new File(filePath);
        if(!fileDir.exists()){
            fileDir.mkdirs();
        }
        
        byte[] templateDoc = Files.readAllBytes(Paths.get("D:/HkAppDoc/jwFile/workDiscuss.doc")); // 模板文件
        InputStream is = new ByteArrayInputStream(templateDoc);
        Document doc = new Document(is);
        
        Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
        Row baseRow = table.getRows().get(5); //基础行
        
//        System.out.println(table.getCount()); // 10
//        System.out.println(baseRow.getCount()); // 2
//        System.out.println(baseRow.getCells().getCount()); // 2
        baseRow.getCells().get(1).remove();
        
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        doc.save(out, SaveOptions.createSaveOptions(SaveFormat.DOCX));
        FileOutputStream fos = new FileOutputStream(filePath + fileName);
        fos.write(out.toByteArray());
        fos.close();
	}
	
	/**
	 * 可行
	 * @throws Exception
	 */
	@Test 
	public void test2() throws Exception {
		String fileName = "工作讨论-AWG12742.doc";
		String filePath = "D:/HkAppDoc/jwFile/jg_1/tmp/workDiscuss/";
        byte[] templateDoc = Files.readAllBytes(Paths.get("D:/HkAppDoc/jwFile/workDiscuss.doc")); // 模板文件
        InputStream is = new ByteArrayInputStream(templateDoc);
        Document doc = new Document(is);
        
        Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
//        Row baseRow = table.getRows().get(5); //基础行
//        table.getRows().forEach(item->{
//        	if(item.getCells().getCount()<2) {
//        		return;
//        	}
//        	// 合并单元格
//        	Cell cell = item.getCells().get(0);
//        	Cell nextCell = item.getCells().get(1);
//        	cell.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
//        	nextCell.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
//        });
        
        int num = table.getRows().getCount();
        for(int i=5; i< num; i++) {
        	Row item = table.getRows().get(i);
        	System.out.println(i+", "+ item.getText());
        	
        	if(item.getCells().getCount() < 2) {
        		continue;
        	}
        	Cell cell = item.getCells().get(0);
        	Cell nextCell = item.getCells().get(1);
        	cell.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
        	nextCell.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
        }
        
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        doc.save(out, SaveOptions.createSaveOptions(SaveFormat.DOCX));
        FileOutputStream fos = new FileOutputStream(filePath + fileName);
        fos.write(out.toByteArray());
        fos.close();
	}

}
